#!/bin/bash
set -e
set -x

pkg="$1"

if [ ! -d "$pkg" ]; then
	echo "$pkg does not exist" >&2
	exit 1
fi

update_config_sub () {
	local pkg="$1"
	local thedir="$2"
	echo "Updating $pkg config.{sub,guess}"
	cp -af /usr/share/gnu-config/config.sub "$thedir"
	cp -af /usr/share/gnu-config/config.guess "$thedir"
	git add "$thedir"
	cat > cmsg.$$.txt <<-EOF
	${pkg}: updated config.{guess,sub} for LoongArch support
	EOF
	git commit -F cmsg.$$.txt
	rm -f cmsg.$$.txt
}

for config_sub in `git ls-files "${pkg}"'**/config.sub' "${pkg}/config.sub"`; do
	thedir="${config_sub%/config.sub}"
	if ! grep -q -e 'loongarch' "$config_sub"; then
		update_config_sub "$pkg" "$thedir"
	fi
done
